package com.powernode.model;

import com.powernode.constant.BusinessEnum;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

/**
 *
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
@ApiOperation("统一响应结果对象")
public class Result<T> implements Serializable {
    // 默认成功
    private Integer code = 200;

    private String msg = "ok";

    private T data;

    /**
     * 操作成功 , 反馈data数据
     *
     * @param data
     * @param <T>
     * @return
     */
    public static <T> Result<T> success(T data) {
        Result<T> result = new Result<>();
        result.setData(data);
        return result;
    }

    /**
     * 操作失败 , 反馈 状态码 和 错误信息
     * @param code
     * @param msg
     * @return
     * @param <T>
     */
    public static <T> Result<T> fail(Integer code, String msg) {
        return (Result<T>) Result.builder().code(code).msg(msg).data(null).build();
    }

    /**
     * 操作失败 , 反馈 响应错误状态码枚举类中的指定信息
     * @param businessEnum
     * @return
     * @param <T>
     */
    public static <T> Result<T> fail(BusinessEnum businessEnum) {
        Result<T> result = new Result<>();
        result.setCode(businessEnum.getCode());
        result.setMsg(businessEnum.getMsg());
        return result;
    }

}
